package ua.kratik.seabattle.web;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ua.kratik.seabattle.dao.api.*;
import ua.kratik.seabattle.domain.*;
import ua.kratik.seabattle.service.api.*;

@Controller
@RequestMapping("/data")
public class DataController {
	
	private Logger logger = LoggerFactory.getLogger(DataController.class);

	@Autowired
	LayoutTypeService layoutTypeService;
	
	@Autowired
	UserService userService;
	
	@Autowired 
	CountryService countryService;
	
	@Autowired
	ShipDirectionService shipDirectionService;
	
	@Autowired
	ShipTypeService shipTypeService;
	
	@Autowired
	ShipService shipService;
	
	@Autowired
	ShipLayoutService shipLayoutService;
	
//	@InitBinder
//	public void addUserInitBinder(WebDataBinder binder){
//		binder.setAutoGrowNestedPaths(true);
//	}
	
	public static final String DATA_VIEWS = "data";
	
	@RequestMapping("")
	public String mainDataPage(){
		return DATA_VIEWS + "/main";
	}
	
	//=============== LayoutType =====================
	
	@RequestMapping("/layout_types.htm")
	public String listLayoutTypes(Map<String, Object> map){
		logger.debug("layout_types.htm");
		map.put("layoutType", new LayoutType());
		try{
			map.put("layoutTypeList", layoutTypeService.findAll());
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return DATA_VIEWS + "/layoutTypes";
	}
	
	@RequestMapping("layout_types/add")
	public String addLayoutType(@ModelAttribute("layoutType") LayoutType layoutType,
			  BindingResult result){
		layoutTypeService.add(layoutType);
		return "redirect:/data/layout_types.htm";
	}
	
	@RequestMapping("layout_types/delete/{layoutTypeId}")
	public String deleteLayoutType(@PathVariable("layoutTypeId") Integer id){
		layoutTypeService.removeById(id);
		return "redirect:/data/layout_types.htm";
	}
	
	//================= Ship Direction ==========================
	
	@RequestMapping("ship_directions")
	public String listShipDirections(Map<String, Object> map){
		map.put("shipDirection", new ShipDirection());
		try{
			map.put("shipDirectionList", shipDirectionService.findAll());
		}catch(Exception ex){
			logger.error("ship_directions",ex);
		}
		List<ShipDirection> list = shipDirectionService.findAll();
		Iterator<ShipDirection> it = list.iterator();
		while(it.hasNext()){
			System.out.println(it.next().getDirection());
		}
		return DATA_VIEWS + "/shipDirections";
	}
	
	@RequestMapping("ship_directions/add")
	public String addShipDirection(@ModelAttribute("shipDirection") ShipDirection direction,
			  BindingResult result){
		shipDirectionService.add(direction);
		return "redirect:/data/ship_directions.htm";
	}
	
	@RequestMapping("ship_directions/delete/{shipDirectionId}")
	public String deleteShipDirection(@PathVariable("shipDirectionId") Integer id){
		shipDirectionService.removeById(id);
		return "redirect:/data/ship_directions.htm";
	}
	
	
	
	//================= Ship Type ==========================
	
	@RequestMapping("ship_types")
	public String listShipTypess(Map<String, Object> map){
		map.put("shipType", new ShipType());
		try{
			map.put("shipTypeList", shipTypeService.findAll());
		}catch(Exception ex){
			logger.error("ShipType saving error",ex);
		}
		return DATA_VIEWS + "/shipTypes";
	}
	
	@RequestMapping("ship_types/add")
	public String addShipType(@ModelAttribute("shipType") ShipType shipType,
			  BindingResult result){
		shipTypeService.add(shipType);
		return "redirect:/data/ship_types.htm";
	}
	
	@RequestMapping("ship_types/delete/{shipTypeId}")
	public String deleteShipType(@PathVariable("shipTypeId") Integer id){
		shipTypeService.removeById(id);
		return "redirect:/data/ship_types.htm";
	}


	//================= Ship ==========================
	
	@RequestMapping("ships")
	public String listShips(Map<String, Object> map){
		map.put("ship", new Ship());
		map.put("shipTypes", shipTypeService.findAll());
		try{
			map.put("shipList", shipService.findAll());
		}catch(Exception ex){
			logger.error("Ship saving error",ex);
		}
		return DATA_VIEWS + "/ships";
	}
	
	@RequestMapping("ships/add")
	public String addShip(@ModelAttribute("ship") Ship ship,
			  BindingResult result){
		shipService.add(ship);
		return "redirect:/data/ships.htm";
	}
	
	@RequestMapping("ships/delete/{shipId}")
	public String deleteShip(@PathVariable("shipId") Integer id){
		shipService.removeById(id);
		return "redirect:/data/ships.htm";
	}

	
	//================= Ship Layout ==========================
	
	@RequestMapping("ship_layouts")
	public String listShipLayouts(Map<String, Object> map){
		map.put("shipLayout", new ShipLayout());
		//map.put("battles", battleService.findAll());
		map.put("users", userService.findAll());
		map.put("ships", shipService.findAll());
		map.put("directions", shipDirectionService.findAll());
		try{
			map.put("shipLayoutList", shipLayoutService.findAll());
		}catch(Exception ex){
			logger.error("Ship layout error",ex);
		}
		return DATA_VIEWS + "/shipLayouts";
	}
	
	@RequestMapping("ship_layouts/add")
	public String addShipLayout(@ModelAttribute("shipLayout") ShipLayout shipLayout,
			  BindingResult result){
		shipLayoutService.add(shipLayout);
		return "redirect:/data/ship_layouts.htm";
	}
	
	@RequestMapping("ship_layouts/delete/{shipLayoutId}")
	public String deleteShipLayout(@PathVariable("shipLayoutId") Integer id){
		shipService.removeById(id);
		return "redirect:/data/ship_layouts.htm";
	}
	
	

	//================= Country ==========================
	
	@RequestMapping("countries")
	public String listCountries(Map<String, Object> map){
		map.put("country", new Country());
		try{
			map.put("countryList", countryService.findAll());
		}catch(Exception ex){
			logger.error("Countries searching error",ex);
		}
		return DATA_VIEWS + "/countries";
	}
	
	@RequestMapping("countries/add")
	public String addCountry(@ModelAttribute("country") Country country,
			  BindingResult result){
		countryService.add(country);
		return "redirect:/data/countries.htm";
	}
	
	@RequestMapping("countries/delete/{countryId}")
	public String deleteCountry(@PathVariable("countryId") Integer id){
		countryService.removeById(id);
		return "redirect:/data/countries.htm";
	}

		
//================= User ==============================	
	
	@RequestMapping("users")
	public String users(Map<String, Object> map){
		map.put("user", new User());
		map.put("userList", userService.findAll());
		map.put("countries", countryService.findAll());
		return DATA_VIEWS + "/users";
	}
	
	@RequestMapping(value = "users/add", method = RequestMethod.POST)
	public String addUser(@ModelAttribute("user") User user,
							  BindingResult result){
		userService.add(user);
		return "redirect:/data/users.htm";
	}
	
	@RequestMapping("users/delete/{userId}")
	public String deleteUser(@PathVariable("userId") Integer userId){
		userService.removeById(userId);
		return "redirect:/data/users.htm" ;
	}


}
